package com.example.administrator.yuejian23.Activity;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian23.Bean.TrainInfo;
import com.example.administrator.yuejian23.Const;
import com.example.administrator.yuejian23.R;
import com.example.administrator.yuejian23.Retrofit.RetrofitService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class TrainDetailActivity extends Activity {
    private Button enter;
    private String t_id;
    private ImageView pic;
    private TextView train_detail_title,train_detail_intro,train_detail_notify,
            train_detail_time,train_detail_address,train_detail_price;
    private Retrofit retrofit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_detail);
        pic=(ImageView)findViewById(R.id.train_detail_pic);
        train_detail_title=(TextView)findViewById(R.id.train_detail_title);
        train_detail_time=(TextView)findViewById(R.id.train_detail_time);
        train_detail_notify=(TextView)findViewById(R.id.train_detail_notify);
        train_detail_address=(TextView)findViewById(R.id.train_detail_address);
        train_detail_intro=(TextView)findViewById(R.id.train_detail_intro);
        train_detail_price=(TextView)findViewById(R.id.train_detail_price);
        enter=(Button)findViewById(R.id.train_detail_enter);
        Intent intent=getIntent();
        t_id=intent.getStringExtra("train_id");
          Retrofit  retrofit = new Retrofit.Builder().
                baseUrl(Const.BASE_URL).
                addConverterFactory(GsonConverterFactory.create()).
                build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<TrainInfo> call=service.getTrainInfo(t_id);
        call.enqueue(new Callback<TrainInfo>() {
            @Override
            public void onResponse(Call<TrainInfo> call, Response<TrainInfo> response) {
                TrainInfo info=response.body();
                train_detail_price.setText("￥"+info.getPrice());
                train_detail_title.setText(info.getName());
                train_detail_intro.setText(info.getIntro());
                String notice=info.getNotice().replaceAll("<p>","").replaceAll("</p>","");
                train_detail_notify.setText(notice);
                train_detail_time.setText(info.getTraintime());
                train_detail_address.setText(info.getPlace());
                Picasso.with(TrainDetailActivity.this).load(Const.PIC_URL+info.getThumb()).into(pic);
            }

            @Override
            public void onFailure(Call<TrainInfo> call, Throwable t) {
                Toast.makeText(TrainDetailActivity.this,"详情获取失败",Toast.LENGTH_SHORT).show();
            }
        });
            enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(TrainDetailActivity.this,EnterActivity.class);
                startActivity(intent);
            }
        });

    }




}
